package cn.xiehang.SoundSight;

import android.media.MediaPlayer;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.io.IOException;
import java.util.List;

import okhttp3.Callback;

public class ArticleAdapter extends RecyclerView.Adapter< ArticleAdapter. ArticleViewHolder> {
    List<Article> articleList;
    String getImageUrl=SeverURL.URL+"/get_image/";
    String getAudioUrl=SeverURL.URL+"/get_audio/";

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(Article article);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }





    @NonNull
    @Override
    public ArticleAdapter.ArticleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_article, parent, false);

        return new ArticleViewHolder(view);
    }
    public ArticleAdapter(List<Article> articleList) {
        this.articleList = articleList;
    }

    @Override
    public void onBindViewHolder(@NonNull ArticleViewHolder holder, int position) {
        Article article = articleList.get(position);


        holder.tvArticleTitle.setText(article.getTitle());
        holder.btnArticleVoice.setVisibility(article.isHaveAudio() ? View.VISIBLE : View.GONE);

        // 使用Glide加载图片
        Glide.with(holder.itemView.getContext())
                .load(getImageUrl+article.getImageUrl())
                .placeholder(R.drawable.background)
                .error(R.drawable.background) // 添加错误占位图
                .into(holder.ivArticleImage);
        holder.btnArticleVoice.setOnClickListener(v -> {
            if (holder.btnArticleVoice.getText().equals("播放中...")) {
                Toast.makeText(holder.itemView.getContext(), "音频正在播放", Toast.LENGTH_SHORT).show();
                return; // 如果正在播放，则不执行
            }

            try {
                // 初始化MediaPlayer
                android.media.MediaPlayer mediaPlayer = new android.media.MediaPlayer();

                // 设置音频源
                mediaPlayer.setDataSource(getAudioUrl+article.getAudioUrl());

                // 准备播放
                mediaPlayer.prepare();

                // 开始播放
                mediaPlayer.start();

                // 设置播放完成监听器
                mediaPlayer.setOnCompletionListener(mp -> {
                    mp.release(); // 播放完成后释放资源
                    holder.btnArticleVoice.setText("播放音频"); // 恢复按钮文字
                });

                holder.btnArticleVoice.setText("播放中..."); // 更新按钮文字



            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(holder.itemView.getContext(), "播放失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.ivArticleImageLayout.setOnClickListener(v -> {
            // 点击图片时的操作
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(article);
            }

        });
    }

    @Override
    public int getItemCount() {
        return articleList != null ? articleList.size() : 0;
    }
    // 播放音频方法


    static class ArticleViewHolder extends RecyclerView.ViewHolder {
        ImageView ivArticleImage;
        Button btnArticleVoice;
        TextView tvArticleTitle;
        LinearLayout ivArticleImageLayout;

        public ArticleViewHolder(@NonNull View itemView) {
            super(itemView);
            ivArticleImage = itemView.findViewById(R.id.article_recycler_image);
            btnArticleVoice = itemView.findViewById(R.id.article_recycler_voice_button);
            tvArticleTitle = itemView.findViewById(R.id.article_recycler_title);
            ivArticleImageLayout = itemView.findViewById(R.id.article_recycler_image_layout);
        }
    }



}
